const formatTime = (date) => {
	const year = date.getFullYear();
	const month = date.getMonth() + 1;
	const day = date.getDate();
	const hour = date.getHours();
	const minute = date.getMinutes();
	const second = date.getSeconds();
	return `${[year, month, day].map(formatNumber).join('-')} ${[hour, minute, second].map(formatNumber).join(':')}`;
};
const formatHourTime = (date) => {
	const year = date.getFullYear();
	const month = date.getMonth() + 1;
	const day = date.getDate();
	const hour = date.getHours();
	const minute = '00';
	return `${[year, month, day].map(formatNumber).join('-')} ${[hour, minute].map(formatNumber).join(':')}`;
};

const formatNumber = (n) => {
	n = n.toString();
	return n[1] ? n : `0${n}`;
};

const formatDate = (date) => {
	const year = date.getFullYear();
	const month = date.getMonth() + 1;
	const day = date.getDate();
	const hour = date.getHours();
	const minute = date.getMinutes();
	const second = date.getSeconds();
	return `${[year, month, day].map(formatNumber).join('-')}`;
};

const formatMonth = (date) => {
	const year = date.getFullYear();
	const month = date.getMonth() + 1;
	return `${[year, month].map(formatNumber).join('-')}`;
};

//时间戳转 时间字符串
const timeTransformat = function(time, type) {
	var date = new Date();
	date.setTime(time * 1000);
	if (type == 'date') {
		return formatDate(date)
	} else if ('month') {
		return formatMonth(date)
	} else {
		return formatTime(date)
	}
};

//时间转时间戳
const transformatToString = function(time) {
	return new Date(time).getTime() / 1000;
};


// 防止处理多次点击
const noMultipleClicks = function(methods, info) {
	// methods是需要点击后需要执行的函数， info是点击需要传的参数
	let that = this;
	if (that.functionName != methods || that.info != info) {
		that.noClick = true;
	};
	if (that.noClick) {
		// 第一次点击
		that.noClick = false;
		that.functionName = methods;
		that.info = info;
		if (info && info !== '') {
			// info是执行函数需要传的参数
			methods(info);
		} else {
			methods();
		}
		setTimeout(() => {
			that.noClick = true;
		}, 1000)
	} else {
		// 这里是重复点击的判断
		uni.showToast({
			title: '请稍后操作',
			icon: 'none'
		});
	}
};

const serializeData = function(data) {
	let arr = [];

	function keyName(key, name) {
		return name ? `${name}[key]` : key;
	}

	const serialize = (obj, name) => {
		for (let key in obj) {
			if (obj[key] === undefined) continue;
			switch (Object.prototype.toString.call(obj[key])) {
				case "[object Object]":
					serialize(obj[key], key);
					break;
				case "[object Array]":
					let array = obj[key];
					for (let i = 0; i < array.length; i++) {
						arr.push(keyName(key, name) + "=" + array[i]);
					}
					break;
				default:
					arr.push(keyName(key, name) + "=" + obj[key]);
			}
		}
	};

	serialize(data);
	return arr.join("&")
}
// 获取当前月份的第一天
const getCurrentMonthFirst = function(_date) {
	var date = new Date(_date)
	date.setDate(1)
	var month = parseInt(date.getMonth() + 1)
	var day = date.getDate()
	if (month < 10) {
		month = '0' + month
	}
	if (day < 10) {
		day = '0' + day
	}
	return date.getFullYear() + '-' + month + '-' + day
}

// 获取当前月份的最后一天
const getCurrentMonthLast = function(_date) {
	var date = new Date(_date)
	var currentMonth = date.getMonth()
	var nextMonth = ++currentMonth
	var nextMonthFirstDay = new Date(date.getFullYear(), nextMonth, 1)
	var oneDay = 1000 * 60 * 60 * 24
	var lastTime = new Date(nextMonthFirstDay - oneDay)
	var month = parseInt(lastTime.getMonth() + 1)
	var day = lastTime.getDate()
	if (month < 10) {
		undefined
		month = '0' + month
	}
	if (day < 10) {
		undefined
		day = '0' + day
	}
	return date.getFullYear() + '-' + month + '-' + day
}
module.exports = {
	formatTime,
	formatHourTime,
	formatDate,
	timeTransformat,
	transformatToString,
	noMultipleClicks,
	serializeData,
	getCurrentMonthFirst,
	getCurrentMonthLast
};